using Joy.Erp.BasicArchives.Core.Filters;
using Joy.Erp.BasicArchives.HttpApi.SimplyClient.Core;
using Joy.Erp.BasicArchives.Suppliers;
using System;
using System.Threading.Tasks;

namespace Joy.Erp.BasicArchives.HttpApi.SimplyClient.Suppliers
{
    public class SupplierTypeClientProxy
    {
        public readonly TokenClientProxy TokenClientProxy;

        public SupplierTypeClientProxy(TokenClientProxy tokenClientProxy)
        {
            TokenClientProxy = tokenClientProxy;
        }

        public async Task<SupplierTypeDto> GetAsync(Guid id, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/supplier-type/{id}");

            return await ApiHandler.HttpGetAsync<SupplierTypeDto>(token, url, logfunc);
        }

        public SupplierTypeDto Get(Guid id, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await GetAsync(id, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<SupplierTypeDto> FindAsync(string filter, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/supplier-type/find?filter={filter}");

            return await ApiHandler.HttpGetAsync<SupplierTypeDto>(token, url, logfunc);
        }

        public SupplierTypeDto Find(string filter, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await FindAsync(filter, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<SupplierTypeDto> FindBySourceKeyAsync(string sourceKey, string sourceType, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/supplier-type/find-by-source-key?sourceKey={sourceKey}&sourceType={sourceType}");

            return await ApiHandler.HttpGetAsync<SupplierTypeDto>(token, url, logfunc);
        }

        public SupplierTypeDto FindBySourceKey(string sourceKey, string sourceType, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await FindBySourceKeyAsync(sourceKey, sourceType, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<PagedResultDto<SupplierTypeDto>> GetListAsync(FilterRequestDto input, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/supplier-type?filter={input.Filter}&sorting={input.Sorting}&skipCount={input.SkipCount}&maxResultCount={input.MaxResultCount}");

            return await ApiHandler.HttpGetAsync<PagedResultDto<SupplierTypeDto>>(token, url, logfunc);
        }

        public PagedResultDto<SupplierTypeDto> GetList(FilterRequestDto input, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await GetListAsync(input, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<PagedResultDto<SupplierTypeDto>> QueryAsync(FilterGroupRequestDto input, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/supplier-type/query");

            return await ApiHandler.HttpPostAsync<PagedResultDto<SupplierTypeDto>>(token, url, input, logfunc);
        }

        public PagedResultDto<SupplierTypeDto> Query(FilterGroupRequestDto input, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await QueryAsync(input, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<ListResultDto<SupplierTypeDto>> GetChildrenAsync(Guid? parentId, bool recursive = false, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/supplier-type/get-children");

            var requestData = new { parentId, recursive };
            return await ApiHandler.HttpPostAsync<ListResultDto<SupplierTypeDto>>(token, url, requestData, logfunc);
        }

        public ListResultDto<SupplierTypeDto> GetChildren(Guid? parentId, bool recursive = false, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await GetChildrenAsync(parentId, recursive, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<SupplierTypeDto> CreateAsync(SupplierTypeCreateDto input, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/supplier-type");

            return await ApiHandler.HttpPostAsync<SupplierTypeDto>(token, url, input, logfunc);
        }

        public SupplierTypeDto Create(SupplierTypeCreateDto input, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await CreateAsync(input, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task<SupplierTypeDto> UpdateAsync(Guid id, SupplierTypeUpdateDto input, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/supplier-type/{id}");

            return await ApiHandler.HttpPutAsync<SupplierTypeDto>(token, url, input, logfunc);
        }

        public SupplierTypeDto Update(Guid id, SupplierTypeUpdateDto input, Action<string> logfunc = null)
        {
            try
            {
                return Task.Run(async () => await UpdateAsync(id, input, logfunc)).Result;
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public async Task DeleteAsync(Guid id, Action<string> logfunc = null)
        {
            var token = await TokenClientProxy.GetToken(logfunc);
            var url = TokenClientProxy.Config.ApiUrl.AppendUrl($"api/basic-archives/supplier-type/{id}");

            await ApiHandler.HttpDeleteAsync(token, url, logfunc);
        }

        public void Delete(Guid id, Action<string> logfunc = null)
        {
            try
            {
                Task.Run(async () => await DeleteAsync(id, logfunc));
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }
    }
} 
